package com.gs.synedocket.presenter;

import com.google.gson.reflect.TypeToken;
import com.gs.synedocket.apiresult.ApiResultData;
import com.gs.synedocket.constant.AllConstants;
import com.gs.synedocket.db.dto.DriverSignDto;
import com.gs.synedocket.db.dto.MenuDto;
import com.gs.synedocket.db.dto.VersionDto;
import com.gs.synedocket.model.DocketModel;
import com.gs.synedocket.model.MenuModel;
import com.gs.synedocket.model.SettingsModel;
import com.gs.synedocket.model.UtilModel;
import com.gs.synedocket.model.listener.OnResultListener;
import com.gs.synedocket.model.listener.OnResultProgressListener;
import com.gs.synedocket.util.SPUtil;
import com.gs.synedocket.view.IMainView;

import java.io.File;
import java.lang.reflect.Type;
import java.util.List;

public class MainPresenter extends BasePresenter<IMainView> {
    private MenuModel menuModel = new MenuModel();
    private SettingsModel settingsModel = new SettingsModel();
    private DocketModel docketModel = new DocketModel();
    private UtilModel model = new UtilModel();

    public void getList(String PId) {
        menuModel.menus(PId, "left", new OnResultListener() {
            @Override
            public void onSuccess(Object aipResult) {
                if (getView() != null) {
                    getView().onGetListSuccess((ApiResultData<List<MenuDto>>) aipResult);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().onGetListFails(e);
                }
            }

        }, new TypeToken<ApiResultData<List<MenuDto>>>() {
        }.getType());
    }

    public void logOut() {
        getView().showLoading();
        settingsModel.logOut(SPUtil.getInstance().getString(AllConstants.ID), new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().hideLoading();
                    getView().onLogoutSuccess((ApiResultData<String>) apiResult);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                    getView().onLogoutFails(e);
                }
            }
        }, new TypeToken<ApiResultData<String>>() {
        }.getType());
    }

    public void revertSign(DriverSignDto bean, int newState) {
        if (getView() != null) {
            getView().showLoading();
            Type typeofT = new TypeToken<ApiResultData<String>>() {
            }.getType();
            docketModel.revertSign(bean, new OnResultListener() {
                @Override
                public void onSuccess(Object aipResult) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onRevertSuccess((ApiResultData<String>) aipResult, newState);
                    }
                }

                @Override
                public void onFails(Exception e) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onRevertFails(e);
                    }
                }
            }, typeofT);
        }
    }

    public void getVersion() {
        model.getVersion(new OnResultListener() {
            @Override
            public void onSuccess(Object aipResult) {
                if (getView() != null) {
                    getView().onGetVersionSuccess((ApiResultData<VersionDto>) aipResult);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().onGetVersionFails(e);
                }
            }
        }, new TypeToken<ApiResultData<VersionDto>>() {
        }.getType());
    }

    public void downloadFile(String fileName, File file) {
        model.download(fileName, file, new OnResultProgressListener() {
            @Override
            public void onSuccess(Object aipResult) {
                if (getView() != null) {
                    getView().onDownloadFileSuccess((File) aipResult);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().onDownloadFails(e);
                }
            }

            @Override
            public void onProgress(int progress) {
                if (getView() != null) {
                    getView().onDownloadProgress(progress);
                }
            }
        });
    }

    public void downloadFileMulti(String fileName, File file) {
        model.downloadMulti(fileName, file, new OnResultProgressListener() {
            @Override
            public void onSuccess(Object aipResult) {
                if (getView() != null) {
                    getView().onDownloadFileSuccess((File) aipResult);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().onDownloadFails(e);
                }
            }

            @Override
            public void onProgress(int progress) {
                if (getView() != null) {
                    getView().onDownloadProgress(progress);
                }
            }
        });
    }
}
